package com.servlets;

import com.domain.UserEntity;
import com.service.StudentService;
import com.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        StudentService studentService = new StudentServiceImpl();

        req.setCharacterEncoding("utf-8");

        String sno = req.getParameter("sno");
        String pp = req.getParameter("pwd");

        int snoInt = Integer.parseInt(sno);

        UserEntity student = studentService.login(snoInt, pp);

        if (student != null) {

            // 登录成功
            HttpSession session = req.getSession();
            session.setAttribute("user", student);
            resp.sendRedirect("main.jsp");
        } else {
            // 登录失败
            resp.sendRedirect("loginError.jsp");
        }

    }

}
